<?php
    $item_menu[0] = 5;
    $item_menu[1] = 9 ;
    $title = 'Estadisticas';    
    include('header.php');

	
	$familia_id = request('familia_id',0);
    if (!isset($_SESSION["admin"])) {redirect("login.php"); exit(); }


    $Familias = crearArbol(0);
    $parent_id = 0;
    $mostrar_familia = "<select name='familia_id' onChange=\"document.location.href='estadisticas.php?familia_id='+this.value\">";
    $mostrar_familia.= "<option value='0' >Todas las categorías</option>";
    foreach($Familias as $dd){
            if ($familia_id == $dd['id']) { $sel = "selected='selected'"; } else {$sel='';}
            
            $separador = "";
            if ($dd['nivel'] == 2 ) {
                $separador = "------";
            } elseif ($dd['nivel'] == 3 ) {
                $separador = "------------";
            } elseif ($dd['nivel'] == 4 ) {
                $separador = "------------------";
            }
            $mostrar_familia.= "<option value='{$dd['id']}' $sel >$separador {$dd['nombre1']}</option>";
    }
    $mostrar_familia.= "</select>";

	if($familia_id>0){
		$sql = "select fecha, sum(visualizacion) as visualizacion, sum(click) as click, sum(link) as  link, sum(contacto) as contacto 
				from visitas_unicas as vu
				where producto_id in (select id from productos where familia_id='$familia_id')
				
				group by fecha order by fecha ASC";
		$rs  = $db->Execute($sql);
		$Visitas = $rs->GetRows();
	
	} else {
	
		$sql = "select fecha, sum(visualizacion) as visualizacion, sum(click) as click, sum(link) as  link, sum(contacto) as contacto 
				from visitas_unicas group by fecha order by fecha ASC";
		$rs  = $db->Execute($sql);
		$Visitas = $rs->GetRows();

	}

?>

Familia: <?php echo $mostrar_familia;?><br>


<table id="myTable" class="tablesorter" >
	<thead>
	<tr>
		<th>Período</th>
		<th style='text-align:center;'>Visualizaciones</th>
		<th style='text-align:center;'>Micrositio</th>
		<th style='text-align:center;'>Derivados</th>
		<th style='text-align:center;'>Consultas</th>
	</tr>
	</thead>
	<tbody>
		<?php
			$t_visualiza = 0;
			$t_click     = 0;
			$t_link      = 0;
			$t_contacto  = 0;
		?>
	
	<?php foreach($Visitas as $v){?>
		<tr>
			<td style='text-align:center;'><?php echo $v['fecha'];?></td>
			<td style='text-align:center;'><?php echo number_format($v['visualizacion'],0,',','.');?></td>
			<td style='text-align:center;'><?php echo number_format($v['click'],0,',','.');?></td>
			<td style='text-align:center;'><?php echo number_format($v['link'],0,',','.');?></td>
			<td style='text-align:center;'><?php echo number_format($v['contacto'],0,',','.');?></td>
		</tr>
		<?php
			$t_visualiza = $t_visualiza + $v['visualizacion'];
			$t_click     = $t_click + $v['click'];
			$t_link      = $t_link + $v['link'];
			$t_contacto  = $t_contacto + $v['contacto'];
		?>
	<?php } ?>
	</tbody>
	<tfoot>
		<tr>
			<td style='text-align:right;'>Totales</td>
			<td style='text-align:center;'><?php echo number_format($t_visualiza,0,',','.');?></td>
			<td style='text-align:center;'><?php echo number_format($t_click,0,',','.');?></td>
			<td style='text-align:center;'><?php echo number_format($t_link,0,',','.');?></td>
			<td style='text-align:center;'><?php echo number_format($t_contacto,0,',','.');?></td>
		</tr>
	</tfoot>
</table>
<script type="text/javascript">
	$(document).ready(function() { 
		$("#myTable").tablesorter();
</script>


<script type="text/javascript" src="https://www.google.com/jsapi"></script>

  <div class='grid_8'>
    <h3 style='text-align:left;margin-bottom:20px;'>Visualizaciones totales de anuncio: <?php echo $total_visitas;?></h3>   
    <?php if(!empty($Visitas)) {?>        
        <script type="text/javascript">
          google.load("visualization", "1", {packages:["corechart"]});
          google.setOnLoadCallback(drawChart);
          function drawChart() {
            var data = google.visualization.arrayToDataTable([
              ['Año/Mes', 'Visitas'],
              <?php foreach($Visitas as $v){ ?>
                  ['<?php echo $v["fecha"];?>',  <?php echo $v["visualizacion"];?>],
              <?php } ?>
            ]);


       new google.visualization.LineChart(document.getElementById('chart_div')).
          draw(data, {curveType: "function",
                  title: 'Visualizaciones del Anuncio, dentro del listado general',
                      width: 900, height: 500}
              );

          }
      </script>
      <div id="chart_div" style="width: 900px; height: 500px;"></div>
    <?php } ?>
  </div>






  <div class='grid_8'>
    <h3 style='text-align:left;margin-bottom:20px;'>Visualizaciones totales de su Micrositio: <?php echo $total_clicks;?></h3>    
    <?php if(!empty($Visitas)) {?>        
        <script type="text/javascript">
          google.load("visualization", "1", {packages:["corechart"]});
          google.setOnLoadCallback(drawChart);
          function drawChart() {
            var data = google.visualization.arrayToDataTable([
              ['Año/Mes', 'Visitas'],
              <?php foreach($Visitas as $v){ ?>
                  ['<?php echo $v["fecha"];?>',  <?php echo $v["click"];?>],
              <?php } ?>
            ]);


       new google.visualization.LineChart(document.getElementById('chart_click')).
          draw(data, {curveType: "function",
                  title: 'Visitas por Mes a su Micrositio',
                      width: 900, height: 500}
              );

          }
      </script>
      <div id="chart_click" style="width: 900px; height: 500px;"></div>
    <?php } ?>      
  </div>


  <div class='grid_8'>
    <h3 style='text-align:left;margin-bottom:20px;'>Usuarios enviados a su Web: <?php echo $total_enlaces;?></h3>       
    <?php if(!empty($Visitas)) {?>    
        <script type="text/javascript">
          google.load("visualization", "1", {packages:["corechart"]});
          google.setOnLoadCallback(drawChart);
          function drawChart() {
            var data = google.visualization.arrayToDataTable([
              ['Año/Mes', 'Enlaces'],
              <?php foreach($Visitas as $v){ ?>
                  ['<?php echo $v["fecha"];?>',  <?php echo $v["link"];?>],
              <?php } ?>
            ]);


       new google.visualization.LineChart(document.getElementById('chart_enlaces')).
          draw(data, {curveType: "function",
                  title: 'Usuarios enviados a su web',
                      width: 900, height: 500}
              );

          }
      </script>
      <div id="chart_enlaces" style="width: 900px; height: 500px;"></div>
    <?php } ?>    
  </div>


  <div class='grid_8'>
    <h3 style='text-align:left;margin-bottom:20px;'>Consultas generadas desde <?php echo $DatosEmpresa['nombre_empresa'];?>: <?php echo $total_formularios;?></h3>        
    <?php if(!empty($Visitas)) {?>
        <script type="text/javascript">
          google.load("visualization", "1", {packages:["corechart"]});
          google.setOnLoadCallback(drawChart);
          function drawChart() {
            var data = google.visualization.arrayToDataTable([
              ['Año/Mes', 'Enlaces'],
              <?php foreach($Visitas as $v){ ?>
                  ['<?php echo $v["fecha"];?>',  <?php echo $v["contacto"];?>],
              <?php } ?>
            ]);


       new google.visualization.LineChart(document.getElementById('chart_form')).
          draw(data, {curveType: "function",
                  title: 'Formularios de Consulta generados',
                      width: 900, height: 500}
              );

          }
      </script>
      <div id="chart_form" style="width: 900px; height: 500px;"></div>
    <?php } ?>
  </div>


